# LeetCode 735.行星碰撞
# https://leetcode.cn/problems/asteroid-collision/

from typing import List


class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        stack = []
        for index, asteroid in enumerate(asteroids):
            if index == 0 or not stack:
                stack.append(asteroid)
                continue
            flag = True
            while stack and asteroid < 0 < stack[-1]:
                if abs(stack[-1]) > abs(asteroid):
                    flag = False
                    break
                elif abs(stack[-1]) == abs(asteroid):
                    stack.pop(-1)
                    flag = False
                    break
                else:
                    stack.pop(-1)
            if flag:
                stack.append(asteroid)
        return stack


if __name__ == '__main__':
    asteroids = [8, -8]
    result = Solution().asteroidCollision(asteroids)
    print(result)
